<?php
/**
 * 客源管理
 */
namespace Member\Controller;
use Org\Yike\Member;
class CustController extends CommonController {
    public function _initialize(){
        parent::_initialize();
        $this->is_login();
        $this->status = getStatus();
    }

    public function index(){
        $member = new Member();
        $map[1] = array ('demand'=>1,'state'=>array ('neq',4),'state'=>array ('neq',5));
        $map[2] = array ('demand'=>2,'state'=>array ('neq',4),'state'=>array ('neq',5));
        $list[1] = $member::getCustList(20,$map[1],true);
        $list[2] = $member::getCustList(20,$map[2],true);
        $this->list = $list;
        $this->seoKey(array('seo_title'=>'客源列表'));
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I ('post.data', '');
            $data[area] =  $data['area1']=='0' && $data['area2']=='0'? '0': $data['area1'].'-'.$data['area2'];
            $data[need_type] = $data['need_type1']=='0' && $data['need_type2']=='0'? 0:$data['need_type1'].'-'.$data['need_type2'];
            $data[price] = $data['price1']=='0' && $data['price2']=='0'? 0: $data['price1'].'-'.$data['price2'];
            $data[fool_scope] = $data['fool_scope1']=='0' && $data['fool_scope2']=='0'? 0: $data['fool_scope1'].'-'.$data['fool_scope2'];
            $data['user_id'] = $this->user['id'];
            $data['add_time']  = date("Y-m-d H:i:s");
            if(M('customs')->add($data)){
                //短信通知客户

                $this->success('提交成功，等待客户审核',U ('Cust/index'));
            }else{
                $this->error('客源发布失败');
            }
        }else{
            $this->seoKey(array('seo_title'=>'录入客源'));
            $this->display();
        }
    }

    public function edit(){
        if(IS_POST){
            $id = intval($_POST['id']);
            $customs = M('customs')->where(array('user_id'=>$this->user['id'],'id'=>$id ))->find();
            if(!$customs){
                $this->error('找不到对应的客源信息');
            }

            $data = $_POST['data'];
            //基本信息
            $demand = $data['demand'];
            $title = $data['title'];
            $gender = $data['gender'];
            $contact = $data['contact'];

            if(empty($demand)){
                $this->error('请选择类型');
            }
            if(empty($title)){
                $this->error('请填写联系人');
            }
            if(empty($contact)){
                $this->error('请填写联系电话');
            }

            $data[area] = $data['area1'].'|'.$data['area2'];
            $data[need_type] = $data['need_type1'].'|'.$data['need_type2'];
            $data[price] = $data['price1'].'|'.$data['price2'];
            $data[fool_scope] = $data['fool_scope1'].'|'.$data['fool_scope2'];



            if(empty($data['around'])){
                $data['around'] = '0';
            }
            $data['user_id'] = $this->user['id'];

            $data['update_time']  =  date("Y-m-d H:i:s");

            if(M('customs')->where(array('id'=>$id,'user_id'=>$this->user['id']))->save($data)){
                if($data['status'] == 0){
                    $str = '发布成功,请等候客户审核';
                }elseif($data['status'] == 1){
                    $str = '客源发布成功';
                }else{
                    $str = '客源发布成功';
                }
                $this->success($str);
            }else{echo  M("customs as a")->getLastSql();
                $this->error('客源发布失败');
            }
        }else{
            $info = M('customs a')
                ->field('a.*,es.id as eid,es.title as etitle')
                ->join('left join yk_estate as es on a.estate_id = es.id')
                ->where(array('a.id'=>intval($_GET['id'])))
                ->find();
            $arr = explode('-',$info[area]);
            $info[area1] = $arr[0];
            $info[area2] = $arr[1];

            $arr = explode('-',$info[need_type]);
            $info[need_type1] = $arr[0];
            $info[need_type2] = $arr[1];

            $arr = explode('-',$info[price]);
            $info[price1] = $arr[0];
            $info[price2] = $arr[1];

            $arr = explode('-',$info[fool_scope]);
            $info[fool_scope1] = $arr[0];
            $info[fool_scope2] = $arr[1];
            //var_dump($info);

            $this->assign ('info', $info);
            $this->seoKey(array('seo_title'=>'编辑客源'));
            $this->display();
        }
    }

    public function view(){
        $info = M('customs')->where(array('user_id'=>$this->user['id'],'id'=>intval($_GET['id'])))->find();
        if(!$info){
            $this->error('找不到对应的客源信息');
        }

        $member = new Member();

        $arr = explode('|',$info[area]);
        $info[area1] = $arr[0];
        $info[area2] = $arr[1];

        $arr = explode('|',$info[need_type]);
        $info[need_type1] = $arr[0];
        $info[need_type2] = $arr[1];

        $arr = explode('|',$info[price]);
        $info[price1] = $arr[0];
        $info[price2] = $arr[1];

        $arr = explode('|',$info[fool_scope]);
        $info[fool_scope1] = $arr[0];
        $info[fool_scope2] = $arr[1];
        //var_dump($info);

        $temp =  M('follow');
        $follow_info = $temp->where(array('user_id'=>$this->user['id'],'source_id'=>intval($_GET['id']),'type'=>2))->select();
        //$sql = $temp->getLastsql();
        //var_dump( $sql);
        $this->info = $info;
        $this->follow_info = $follow_info;
        $this->seoKey(array('seo_title'=>'客源详情'));
        $this->display();
    }

    /**
     * 跟进
     */
    public function follow(){
        if(IS_POST){
            $data = $_POST['data'];
            //基本信息
            $source_id = $data['source_id'];//房/客源ID
            $data['type'] = 2;//1 房源 2 客源
            $discuss = $data['discuss'];//跟进说明
            $data['status'] = 1;//1 正常  0禁用

            if(empty($discuss)){
                $this->error('请填写跟进说明');
            }

            $data['user_id'] = $this->user['id'];////跟进人ID

            $data['add_time']  = date("Y-m-d H:i:s");
            if(M('follow')->add($data)){
                if($data['status'] == 0){
                    $str = '客源跟进添加成功';
                }elseif($data['status'] == 1){
                    $str = '客源跟进添加成功';
                }else{
                    $str = '客源跟进添加成功';
                }
                $this->success($str);
            }else{
                $this->error('客源跟进添加失败');
            }
        }else{
            $id = intval($_GET['id']);
            $member = new Member();
            //$this->cate_list = $member::getCate('article_cate',1);
            $this->seoKey(array('seo_title'=>'客源跟进'));
            $this->customs_id = $id;
            var_dump($id );
            $this->display();
        }
    }

    public function del(){
        $info = M('customs')->where(array('user_id'=>$this->user['id'],'id'=>intval($_GET['id'])))->find();
        if(!$info){
            $this->error('找不到对应的客源信息');
        }
        if(M('article')->where(array('id'=>$info['id']))->save(array('status'=>4))){
            $this->success('客源删除成功!');
        }else{
            $this->error('客源删除失败');
        }

    }

    /**
     *
     */
    public function searchEstate(){
        $keyword = $_POST['searchValue'];
        $member = new Member();
        $list = $member::getEstateList(20," a.title like '%".$keyword."%'",true);
        $data = json_encode($list);
        $this->ajaxReturn($data);
        //$this->display();
    }
    //匹配客源名称
    public function ajaxmach (){
        $name = I ('get.name', '');
        $info = M ('customs')->field ('id,title,contact,id_card')->where (array ('title'=>array('like','%'.$name.'%')))->select ();
        if ($info){
            exit (json_encode(array ('state'=>'ok','data'=>$info)));
        }
        exit (json_encode(array ('state'=>'fail','data'=>'')));

    }
    //搜索客源
    public function retrieve (){
        if (IS_POST){
            $data = I ('post.', '');
            $map = array ();
            if (!empty ($data['title'])){
                $map['c.title'] = array ('like', '%'.$data['title'].'%');
            }
            if (!empty ($data['demand'])) $map = array ('demand'=>$data['demand']);
            if (!empty ($data['price'])){
                $price = explode('-', $data['price']);
                if (count ($price) == 2){
                    $map['_string'] = 'c.price like %'.$price[0].'% or c.price like %'.$price[1].'%';
                }else{
                    $map['_string'] = 'c.price like %'.$data['price'].'% ';;
                }
            }
            if (!empty ($data['area'])){
                $area = explode('-', $data['area']);
                if (count ($area) == 2){
                    $map['_string'] = 'c.area like %'.$area[0].'% or c.area like %'.$area[1].'%';
                }else{
                    $map['_string'] = 'c.area like %'.$data['area'].'% ';;
                }
            }
            if (!empty ($data['state'])) $map = array ('state'=>$data['state']);
            if (!empty ($data['need_type'])){
                $need_type = explode('-', $data['need_type']);
                if (count ($need_type) == 2){
                    $map['_string'] = 'c.need_type like %'.$need_type[0].'% or c.need_type like %'.$need_type[1].'%';
                }else{
                    $map['_string'] = 'c.need_type like %'.$data['need_type'].'% ';;
                }
            }
            $table = array ('yk_customs'=>'c');
            $order = $data['term'].' '.$data['order'];
            $model = M ('customs');
            $model->field ('c.id');
            $model->join ('yk_user u ON c.user_id=u.id', 'LEFT');
            $model->table ($table);
            $model->where ($map);
            $count = $model->count ();
            $pager = new \Org\Util\Page($count, 20);
            $arr['count'] = $count;

            $model->field ('c.*,u.username,es.title as etitle');
            $model->join ('yk_user u ON c.user_id=u.id', 'LEFT');
            $model->join ('yk_estate es ON c.estate_id=es.id', 'LEFT');
            $model->table ($table);
            $model->where ($map);
            $model->order ($order);
            $model->limit($pager->firstRow.','.$pager->listRows);

            $arr['list'] = $model->select();
            $arr['page'] = $pager->show();


            $this->assign ('list', $arr);
            $this->display ('list');
        }else{
            $this->display ();
        }
    }


}